package cn.fintecher.gis.config;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.postgis.PostgisNGDataStoreFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ChenChang on 2018/12/7.
 */
@Configuration
public class PostGISDataStoreConfig {
    @Value("${postGIS.dbType}")
    private String dbType;
    @Value("${postGIS.host}")
    private String host;
    @Value("${postGIS.port}")
    private String port;
    @Value("${postGIS.database}")
    private String database;
    @Value("${postGIS.schema}")
    private String schema;
    @Value("${postGIS.username}")
    private String username;
    @Value("${postGIS.password}")
    private String password;

    @Bean
    public DataStore dataStore() throws IOException {
        Map<String, Object> params = new HashMap<>();
        params.put(PostgisNGDataStoreFactory.DBTYPE.key, dbType);
        params.put(PostgisNGDataStoreFactory.HOST.key, host);
        params.put(PostgisNGDataStoreFactory.PORT.key, new Integer(port));
        params.put(PostgisNGDataStoreFactory.DATABASE.key, database);
        params.put(PostgisNGDataStoreFactory.SCHEMA.key, schema);
        params.put(PostgisNGDataStoreFactory.USER.key, username);
        params.put(PostgisNGDataStoreFactory.PASSWD.key, password);
        DataStore postgisDataStore = DataStoreFinder.getDataStore(params);
        return postgisDataStore;
    }


    public Map<String, Object> dataStoreParams() {
        Map<String, Object> params = new HashMap<>();
        params.put(PostgisNGDataStoreFactory.DBTYPE.key, dbType);
        params.put(PostgisNGDataStoreFactory.HOST.key, host);
        params.put(PostgisNGDataStoreFactory.PORT.key, new Integer(port));
        params.put(PostgisNGDataStoreFactory.DATABASE.key, database);
        params.put(PostgisNGDataStoreFactory.SCHEMA.key, schema);
        params.put(PostgisNGDataStoreFactory.USER.key, username);
        params.put(PostgisNGDataStoreFactory.PASSWD.key, password);
        return params;
    }

    public String getDbType() {
        return dbType;
    }

    public String getHost() {
        return host;
    }

    public String getPort() {
        return port;
    }

    public String getDatabase() {
        return database;
    }

    public String getSchema() {
        return schema;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}
